package com.example.controller.validate;

import com.example.pojo.entity.validate.DemoModel;
import com.example.pojo.vo.validate.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2020/9/22 14:11
 */
@RestController
@Slf4j
public class TestController {

    //{"userName":"dd","age":120,"isFalse":true,"birthday":"21010-21-12"}
    /**
     * 出生日期格式不正确
     * 必须为false
     * 年龄不正确
     * @param demo {"userName":"dd","age":120,"isFalse":true,"birthday":"21010-21-12"}
     * @param result
     */
    @RequestMapping("/demo")
    public Result demo(@RequestBody @Validated DemoModel demo){
//        if(result.hasErrors()){
//            result.getAllErrors().forEach(e->{
//                log.info(e.getDefaultMessage());
//            });
//        }
        return Result.OK();
    }

    /**
     * 多参数效验
     * @param demo
     * @param result
     * @param demo2
     * @param result2
     */
    @RequestMapping("/test")
    public Result test(@RequestBody @Valid DemoModel demo,BindingResult result,
                       @RequestBody @Valid DemoModel demo2,BindingResult result2){
        if(result.hasErrors()){
            result.getAllErrors().forEach(e->{
                log.info("result:{}",e.getDefaultMessage());
            });
        }
        if(result2.hasErrors()){
            result2.getAllErrors().forEach(e->{
                log.info("result2:{}",e.getDefaultMessage());
            });
        }
        return Result.OK();
    }
}
